<?php
include_once 'function.php';
include 'register.inc.php';

$email_err = $pass_err = $pass_con_err = $name_err = '';

if (isset($_POST['uemail'])) 
{
	$email = trim($_POST['uemail']);
	if (isset($email) && !empty($email))
	{
		if (!preg_match('/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/', $email))
		{
			$email_err = '邮箱格式不正确';
		}
		else 
		{
			$conn = conn();
			$esc_email = mysql_real_escape_string($email, $conn);
			$sql = "SELECT id FROM accounts WHERE email = '$esc_email'";
			$result = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($result);
			if (isset($row['id']))
			{
				$email_err = '邮箱已经存在了';
			}
		}	
	}
	else 
	{
		$email_err = '邮箱不能为空';
	}
}

if (isset($_POST['upass'])) 
{
	$pass = trim($_POST['upass']);
	if (isset($pass) && !empty($pass))
	{
		if (6 > strlen($pass))
		{
			$pass_err = '密码过短';
		}
	}
	else 
	{
		$pass_err = '密码不能为空';
	}
}

if (isset($_POST['upass_con']))
{
	$pass_con = trim($_POST['upass_con']);
	if (isset($pass_con) && !empty($pass_con))
	{
		if ($pass != $pass_con)
		{
			$pass_con_err = '密码不一致';
		}
	}
	else 
	{
		$pass_con_err = '请再次输入密码';
	}
}

if (isset($_POST['uname']))
{
	$name = trim($_POST['uname']);
	if (isset($name) && !empty($name))
	{
		$conn = conn();
		$esc_name = mysql_real_escape_string($name, $conn);
		$sql = "SELECT id FROM accounts WHERE name = '$esc_name'";
		$result = mysql_query($sql, $conn);
		$row = mysql_fetch_assoc($result);
		if (isset($row['id']))
		{
			$name_err = '用户名已经存在了';
		}
	}
}


if (isset($_POST['uemail']) && '' == $email_err && '' == $pass_err && '' == $pass_con_err && '' == $name_err)
{
	// insert into db
	$conn = conn();
	$esc_email = mysql_real_escape_string($email, $conn);
	$esc_pwd = md5($pass);
	if (empty($name))
	{
		$esc_name = 'NULL';
	}
	else
	{
		$esc_name = "'".mysql_real_escape_string($name, $conn)."'";
	}

	// create random string
	$shuffled = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz');
	$rand = md5(microtime().substr($shuffled, 0, 32));

	$sql = "INSERT INTO accounts(email, name, pwd, active_key) VALUES('$esc_email', $esc_name, '$esc_pwd', '$rand')";
	mysql_query($sql, $conn);
	
	// send activation email
	$name = empty($name) ? substr($email, 0, strpos($email, '@')) : $name;
	send_active_email($email, $name, $rand);

	// redirect to activing email
	header('Location:/active.php?email='.urlencode($email));
	exit;
}
?><!DOCTYPE HTML>
<html>
	<head>
		<link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<meta name="keywords" content="" />
		<meta name="description" content="" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="theme.css" />
	</head>
	<body>
		<div class="wrapper">
			<div class="logo"><a href="/"><?php echo SITE;?></a></div>
			<div class="clear"></div>
			<form action="register.php" method="post">
				<div class="reg_left"><span style="color:red">*</span> 邮箱：</div>
				<div class="reg_right"><input name="uemail" type="text" class="reg_input" value="<?php if(isset($email)) echo $email; ?>" />&nbsp;&nbsp;
<?php 
if ('' != $email_err) 
{
	echo '<span style="color:red">'.$email_err.'</span>';
}
?></div>
				<div class="clear reg_lh"></div>
				<div class="reg_left"><span style="color:red">*</span> 密码：</div>
				<div class="reg_right"><input name="upass" type="password" class="reg_input" value="<?php if(isset($pass)) echo $pass; ?>" />&nbsp;&nbsp;
<?php 
if ('' != $pass_err) 
{
	echo '<span style="color:red">'.$pass_err.'</span>';
}
?></div>
				<div class="clear reg_lh"></div>
				<div class="reg_left"><span style="color:red">*</span> 确认密码：</div>
				<div class="reg_right"><input name="upass_con" type="password" class="reg_input" value="<?php if(isset($pass_con)) echo $pass_con; ?>" />&nbsp;&nbsp;
<?php 
if ('' != $pass_con_err) 
{
	echo '<span style="color:red">'.$pass_con_err.'</span>';
}
?></div>
				<div class="clear reg_lh"></div>
				<div class="reg_left">用户名：</div>
				<div class="reg_right"><input name="uname" type="text" class="reg_input" value="<?php if(isset($name)) echo $name; ?>" />&nbsp;&nbsp;
<?php 
if ('' != $name_err) 
{
	echo '<span style="color:red">'.$name_err.'</span>';
}
?></div>
				<div class="clear reg_lh"></div>
				<div class="reg_left"></div>
				<div class="reg_right">
					<span style="color:#0000ff; margin-top:10px;text-align:center;"><span style="color:red">*</span> 表示为必填项</span><br />
					<input type="submit" value="注册" class="reg_submit" />
				</div>
				<div class="clear"></div>
			</form>
			<div class="reg_left"></div>
			<div class="reg_right">
				<div style="margin-top:40px;">已经拥有<?php echo SITE;?>帐号？<a href="login.php">直接登录</a></div>
			</div>
			<div class="clear"></div>
		</div>
	</body>
</html>